import request from '@/utils/request';

/**
 * 分页查询代理
 * @param params 查询条件
 */
export async function pageMerchant(params) {
  const res = await request.post('/business/oemAgent/page', params);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 分页查询代理类型
 * @param params 查询条件
 */
export async function pageAgencyType(params) {
  const res = await request.post('/business/agentType/pageList', params);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 分页查询日志
 * @param params 查询条件
 */
export async function logPage(params) {
  const res = await request.post('/business/wjykbisinesslogmanagement/page', params);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加日志
 * @param params 查询条件
 */
export async function mealAdd(params) {
  const res = await request.post('/business/wjykbisinesslogmanagement/add', params);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改日志
 * @param data 商户信息
 */
export async function mealUpdate(data) {
  const res = await request.post('/business/wjykbisinesslogmanagement/update', data);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 删除套餐
 * @param id 用户id
 */
export async function mealDelete(params) {
  const res = await request.post('/business/wjykbisinesslogmanagement/delete', params);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}



/**
 * 批量删除套餐
 * @param data 用户id集合
 */
export async function removeMeal(data) {
  const res = await request.post('/business/wjykbisinesslogmanagement/delete', data);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}


/**
 * 分页查询充值记录
 * @param params 查询条件
 */
export async function rechargeRecord(params) {
  const res = await request.post('/business/oemAgent/rechargeRecord', params);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询用户列表
 * @param params 查询条件
 */
export async function listUsers(params) {
  const res = await request.post('/system/pageList', params);
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询用户
 * @param id 用户id
 */
export async function getUser(id) {
  const res = await request.get('/merchantManagement/merchantAccount/' + id);
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加商户
 * @param data 商户信息
 */
export async function addUser(data) {
  const res = await request.post('/business/oemAgent/add', data);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改商户
 * @param data 商户信息
 */
export async function updateUser(data) {
  const res = await request.post('/business/oemAgent/update', data);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 充值余额
 * @param data 商户信息
 */
export async function updateBalance(data) {
  const res = await request.post('/business/oemAgent/updateBalance', data);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 重置密码
 * @param data 商户信息
 */
export async function resetPassword(data) {
  const res = await request.post('/business/oemAgent/resetPassword', data);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}


/**
 * 删除用户
 * @param id 用户id
 */
export async function removeUser(params) {
  const res = await request.post('/business/oemAgent/delete', params);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 删除充值记录用户
 * @param id 用户id
 */
export async function rechargeDelete(params) {
  const res = await request.post('/business/oemAgent/rechargeDelete', params);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 批量删除用户
 * @param data 用户id集合
 */
export async function removeUsers(data) {
  const res = await request.post('/business/oemAgent/delete', data);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改用户状态
 * @param id 用户id
 * @param status 状态
 */
export async function updateUserStatus(id, status) {
  const res = await request.post('/business/oemAgent/updateStatus', {
    id,
    status
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 重置用户密码
 * @param userId 用户id
 * @param password 密码
 * @returns {Promise<string>}
 */
export async function updateUserPassword(params) {
  const res = await request.post('/auth/user/updatePassword', params);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 导入用户
 * @param file excel文件
 */
export async function importUsers(file) {
  const formData = new FormData();
  formData.append('file', file);
  const res = await request.post('/system/user/import', formData);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 检查用户是否存在
 * @param field 检查的字段
 * @param value 字段的值
 * @param id 修改时的id
 */
export async function checkExistence(params) {
  const res = await request.post('/auth/user/existence', params);
  if (res.data.code === 2000 && res.data.data != null) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}
